package pro.softzhang.algo.lc100;

public class LC125_ValidPalindrome {
    public static void main(String[] args) {
        String str = "A man, a plan, a canal: Panama";
        System.out.println(new LC125_ValidPalindrome().isPalindrome3(str));
    }

    public boolean isPalindrome(String s) {
        int hp = 0, tp = s.length() - 1;

        while (hp < tp) {
            char ch = s.charAt(hp);
            if(!Character.isLetterOrDigit(ch)) {
                hp++;
                continue;
            }

            char ph = s.charAt(tp);
            if(!Character.isLetterOrDigit(ph)) {
                tp--;
                continue;
            }

            if(Character.toLowerCase(ch) != Character.toLowerCase(ph)) {
                return false;
            }

            hp++;
            tp--;
        }

        return true;
    }

    public boolean isPalindrome1(String s) {
        int hp = 0, tp = s.length() - 1;

        while (hp < tp) {
            char ch = s.charAt(hp);
            char ph = s.charAt(tp);

            if(!Character.isLetterOrDigit(ch)) {
                hp++;
            } else if(!Character.isLetterOrDigit(ph)) {
                tp--;
            } else {
                if(Character.toLowerCase(ch) != Character.toLowerCase(ph)) {
                    return false;
                }
                hp++;
                tp--;
            }
        }

        return true;
    }

    public boolean isPalindrome3(String s) {
        int p1 = 0, p2 = s.length() - 1;
        while (p1 < p2) {
            char ch1 = s.charAt(p1);
            if(!Character.isLetterOrDigit(ch1)) {
                p1++;
                continue;
            }
            char ch2 = s.charAt(p2);
            if(!Character.isLetterOrDigit(ch2)) {
                p2--;
                continue;
            }
            if (Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) return false;
            p1++;
            p2--;
        }
        return true;
    }




























}
